"""
Case Type   : SQL
Case Name   : 同一schema下创建外表后创建与外表同名sequence，合理报错
Create At   : 2023/05/08
Owner       : cr13
Description :
    1.安装postgres_fdw扩展,创建外部服务器,用户映射,模式
    2.建本地表
    3.创建外表
    4.创建与外表同名sequence
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.pri_userdb = Node(node='PrimaryDbUser')
        self.commsh = CommonSH('PrimaryDbUser')
        self.tb_name1 = 't_ddl_foreign_sequence_045_01'
        self.tb_name2 = 't_ddl_foreign_sequence_045_02'
        self.server = 'se_ddl_sequence_045'
        self.schema = 'sc_ddl_sequence_045'
        self.port = self.pri_userdb.db_port
        self.database = self.pri_userdb.db_name

    def test_create_sequence(self):
        text = "--step1:安装postgres_fdw扩展,创建外部服务器,用户映射,模式" \
               "expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop extension if exists postgres_fdw cascade;" \
            f"create extension if not exists postgres_fdw;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.drop_extension_success, result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_extension_success, result,
                      '执行失败' + text)

        sql_cmd = f"drop server if exists {self.server} cascade;" \
            f"create server {self.server} foreign data wrapper postgres_fdw " \
            f"options (dbname '{self.database}', port '{self.port}');"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.drop_server_success, result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_server_success, result,
                      '执行失败' + text)

        sql_cmd = f"drop user mapping if exists for current_user server " \
            f"{self.server};create user mapping for current_user server " \
            f"{self.server};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.drop_usermapping_success, result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_usermapping_success, result,
                      '执行失败' + text)

        sql_cmd = f"drop schema if exists {self.schema} cascade;" \
            f"create schema {self.schema};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, result,
                      '执行失败' + text)

        text = "--step2:建本地表 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.schema}.{self.tb_name1};" \
            f"create table {self.schema}.{self.tb_name1}(col_1 int, " \
            f"col_2 int, col_3 varchar2 (30));"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, result, '执行失败' +
                      text)

        text = "--step3:创建外表 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop foreign table if exists {self.tb_name2};" \
            f"create foreign table {self.tb_name2} (col_1 int, col_2 int, " \
            f"col_3 varchar2 (30)) server {self.server} options (schema_name " \
            f"'{self.schema}', table_name '{self.tb_name1}');"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, result,
                      '执行失败' + text)

        text = "--step4:建与外表同名sequence expect:合理报错--"
        self.log.info(text)
        sql_cmd = f"drop sequence if exists {self.tb_name2};create " \
            f"sequence {self.tb_name2};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(f'ERROR:  "{self.tb_name2}" is not a sequence', result,
                      '执行失败' + text)
        self.assertIn(f'ERROR:  relation "{self.tb_name2}" already exists '
                      f'in schema "public"', result, '执行失败' + text)

    def tearDown(self):
        text = "--step5:清理环境 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop extension if exists postgres_fdw cascade; " \
            f"drop server if exists {self.server} cascade;" \
            f"drop user mapping if exists for current_user server " \
            f"{self.server};drop schema if exists {self.schema} cascade;" \
            f"drop foreign table if exists {self.tb_name2};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.drop_extension_success, result,
                      '执行失败' + text)
        self.assertIn(self.constant.drop_server_success, result,
                      '执行失败' + text)
        self.assertIn(self.constant.drop_usermapping_success, result,
                      '执行失败' + text)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, result,
                      '执行失败' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
